<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover">
      <div class="system-menu-search mb15">
        <el-input  v-model="state.keywords" size="default" placeholder="请输入相册名称" style="max-width: 180px"> </el-input>
        <el-button size="default" type="primary" class="ml10" @click="getTableData()">
          <el-icon>
            <ele-Search />
          </el-icon>
          搜索
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="onOpenAddDialog">
          <el-icon>
            <ele-FolderAdd />
          </el-icon>
          新建相册
        </el-button>
      </div>
    <el-table :data="state.tableData.data" stripe style="width: 100%">
      <el-table-column type="index"></el-table-column>
      <el-table-column label="创建者" prop="user.name"></el-table-column>
      <el-table-column label="标签" prop="tag" min-width="125px">
        <template #header>
          <el-select
            v-model="state.mytag"
            @change="getTableData()"
            size="small"
            style="width: 100px"
          >
            <el-option
              v-for="item in state.tags"
              :key="item.value"
              :label="item.text"
              :value="item.value"
            />
            <el-option
                key="0"
                label="全部"
                :value="0"
            />

          </el-select>

        </template>
        <template #default="scope">
          <el-tag type="success" disable-transitions>{{ scope.row.albumType.name }}</el-tag>
        </template>
      </el-table-column>


      <el-table-column label="相册名称" prop="name" min-width="150px">

      </el-table-column>

      <el-table-column label="相册描述" prop="description" min-width="200px"></el-table-column>
      <el-table-column label="相片数量" prop="number"></el-table-column>
      <el-table-column label="状态" prop="status">
        <template #default="scope">
          {{ scope.row.status ? "公开" : "私有" }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" min-width="100px" />
      <el-table-column label="操作" width="250px">
        <template #default="scope">
          <el-button size="small" type="primary" @click="onOpenEditUser('edit', scope.row)">编辑</el-button>
          <el-button size="small" type="primary" @click="goalbums(scope.row.id, scope.row.number, scope.row.name)">管理照片</el-button>
          <el-button size="small" type="danger" @click="onRowDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        size="small"
      @size-change="onHandleSizeChange"
      @current-change="onHandleCurrentChange"
      class="mt15"
      :pager-count="5"
      :page-sizes="[10, 20, 30]"
      v-model:current-page="state.tableData.param.pageNum"
      background
      v-model:page-size="state.tableData.param.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="state.tableData.total"
    >
    </el-pagination>
    </el-card>
    <AlbumDialog ref="homeDialogRef" @refresh="getTableData()" />
  </div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, reactive, onMounted, ref } from "vue";
import { ElMessageBox } from "element-plus";
import { delAlbum, getAlbums } from "/@/views/album/album.ts";
import router from "/@/router";
// 引入组件
const AlbumDialog = defineAsyncComponent(() => import("/@/views/album/dialog.vue"));


const goalbums = (id, total, name) => {
  // activeName.value = '2';
  router.push({ path: "/editalbum", query: { id, total, tagsViewName: name } });
};
// 定义变量内容
const homeDialogRef = ref();
const state = reactive({
  tags: [],
  users: [],
  photoNum: [],
  keywords: "",
  mytag: 0,
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10
    }
  }
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  getAlbums(
    {
      page: state.tableData.param.pageNum,
      pageSize: state.tableData.param.pageSize,
      keywords: state.keywords,
      mytag: state.mytag,
      show: 0
    }
  ).then((res: any) => {
    console.log(res);
    state.tableData.data = res.data.albums;
    state.tags = res.data.albumType.map((item: any) => {
      return {
        value: item.id,
        text: item.name
      };
    });

    state.photoNum = res.data.photoNum;
    state.tableData.total = res.data.total;

  });
  setTimeout(() => {
    state.tableData.loading = false;
  }, 500);
};
// 打开新增用户弹窗
const onOpenAddDialog = (type: string) => {
  homeDialogRef.value.openDialog(type,null, state.tags);
};
// 打开修改用户弹窗
const onOpenEditUser = (type: string, row: any) => {
  homeDialogRef.value.openDialog(type, row, state.tags, state.users);
};
// 删除用户
const onRowDel = (row: any) => {
  ElMessageBox.confirm(`此操作将永久删除"${row.name}"，是否继续?`, "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      delAlbum(row.id).then((res: any) => {
        getTableData();
      });

      // ElMessage.success("删除成功");
    })
    .catch(() => {
    });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {


  getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  padding: 0px;

  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}
</style>
